<?php
/**
 * Created by PhpStorm.
 * User: BlueIdeaLirui
 * Date: 2019/8/20
 * Time: 15:08
 */

namespace console\controllers\marketDataAnalysis;

use business\datacenter\services\LandCronService;
use business\elasticsearch\repositories\LandRepository;
use business\elasticsearch\repositories\LandRepository as EsLandRepository;
use yii\console\Controller;

class LandController extends Controller
{
    /**
     * 市场数据同步
     * php yii marketDataAnalysis/land/process-land-data-rsync-to-es
     * 参数分别传入1,2,3 （拿地数据，开工数据，项目数据）
     * $isCreateIndex 传入0则不再创建索引
     */
    public function actionProcessLandDataRsyncToEs($type = 1, $isCreateIndex = 1, $page = 0)
    {
        try {
            $landCronService = new LandCronService();
            $landCronService->processLandDataRsyncToEs($type, $isCreateIndex, $page);
            echo date('Y-m-d H:i:s') . "拿地数据/开工数据/项目数据同步到es完成\n";
        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            echo '拿地数据/开工数据/项目数据同步到es出错，错误信息：'.$ex->getMessage();
        }
    }

    /**
     * 数据同步调试
     * @throws \yii\db\Exception
     */
    public function actionTest()
    {
        try {
            $value = (new \business\datacenter\repositories\LandRepository())->getLandConstructionOne(['uid' => '85643']);
            $value['company_name'] = '中国联合网络通信有限公司新疆维吾尔族自治区分公司';
            $pattern = '/(\d+\.?\d?)/';
            if(preg_match_all($pattern, $value['contract_price'], $match)){
                // 去掉万元字符
                $value['contract_price'] = $match[0][0];
            } else {
                $value['contract_price'] = '';
            }
            // 如果没有设置时间则初始化日期为1970-01-01 00:00:00
            if ($value['issuing_time'] == '0000-00-00 00:00:00') {
                $value['issuing_time'] = '1970-01-01 00:00:00';
            }
            var_dump($value);
            (new EsLandRepository())->saveLandConstructionRecord($value, $value['uid']);
        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            echo '拿地数据/开工数据/项目数据同步到es出错，错误信息：'.$ex->getMessage();
        }
    }
}
